package com.xueke.crawler.service.createLeafDocRelation;


import com.xueke.crawler.helper.HttpHelper;
import com.xueke.crawler.model.SubjectTree;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * Created by weibo on 2017/5/13.
 *
 * @author weibo.qin
 * @version 2017/5/13
 */
@Component("otherDocRelation")
public class OtherDocRelation extends BaseCreateDocRelation{

    @Override
    public void start() {
        int pageSize = 10;
        int p = 0;
        for (int i = 0; ; i++) {
            List<SubjectTree> list = subjectTreeMapper.queryOtherSubjectTree(i * pageSize, pageSize);

            if (list.size() == 0) {
                System.out.println("其他科目关系表执行完毕！");
                break;
            }

            for (int j = 0; j < list.size(); j++) {
                Integer nodeId = list.get(j).getNid();
                Integer pnId = list.get(j).getPnid();
                Integer cId = list.get(j).getCid();
                Integer isDeaf = list.get(j).getIsDeaf();
                String uri = list.get(j).getDomain() +"/"+ list.get(j).getUri();
                System.out.println(cId + "-----" + pnId + "------" + nodeId + "--------------" + uri + "-----" + p++);
                try {
                    parseSingleLink(uri, list.get(j).getDomain(), nodeId, pnId, cId, isDeaf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private void parseSingleLink(String tUrl, String domain, int leafId, Integer pnId, Integer cId, Integer isDeaf)
        throws IOException {
        for (int i = 1; ; i++) {
            String nextUrl = tUrl + "&page=" + i;
            String result = crawle(domain, leafId, nextUrl, pnId, cId, isDeaf);
            if (StringUtils.isBlank(result)||i>Integer.parseInt(result)) {
                System.out.println("节点=" + leafId + ", 题目采集完毕，已经到达尾页-------");
                break;
            }
        }
    }


    private String crawle(String domain, int leafId, String url, Integer pnId, Integer cId, Integer isDeaf)
        throws IOException {
        System.out.println(url);
        Document doc = HttpHelper.getSimpleDocument(url);
        if(doc==null){
            return "9999999";
        }
        Elements elms = doc.select(".list-cont >div");
        String maxPage = doc.select("#maxLoag").text();
        if (elms.size() == 1) return null;
        for (Element elm : elms) {
            String docId = elm.attr("id");
            String docUrl = domain + "/soft/" + docId + ".html";
            String point = elm.select("div.point").text();
            int level =0;
            System.out.println(point);
            if(!StringUtils.isBlank(point)){
                level=parseLevel(point);
            }
            saveLessonRelation(leafId, docId, docUrl, pnId, cId, level);
        }
        return maxPage;
    }

    private String getUrl(String url, int page) {
        return url + "&page=" + page;

    }

    private String getNextUrl(String domain, Document doc) {
        Elements as = doc.select("div.showpageList a");
        for (Element a : as) {
            if (a.text().equals("下一页")) {
                if ("javascript:void(0);".equals(a.attr("href"))) return null;
                return domain + a.attr("href");
            }
        }
        return null;
    }


}
